<?php

namespace App\Http\Controllers\Web\Seller;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Models\OrderExpress;
use App\Repository\Models\Orders;
use App\Repository\Repositories\Interfaces\OrderExpressRepository;
use App\Repository\Repositories\Interfaces\OrderProductsRepository;
use App\Repository\Repositories\Interfaces\OrdersRepository;
use App\Repository\Repositories\Interfaces\UserCommentRepository;
use App\Services\Api\KuaiDi100;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

/**
 * 卖家评价买家控制器
 *
 * @package App\Http\Controllers\Backend
 */
class UserCommentController extends BaseController
{
    /**
     * @var UserCommentRepository
     */
    protected $user_comment_repo;

    protected $shop_id;

    protected $user_id; //买家id

    protected $order_id;

    protected $order_code;

    public function __construct(Request $request,OrdersRepository $orders_repo, UserCommentRepository $user_comment_repo)
    {
        parent::__construct();
        $this->user_comment_repo = $user_comment_repo;
        $this->shop_id = array_get($this->user, 'shop_id');

        //获取order_id
        $this->order_id = route_parameter('order_id', 0);
        $this->order_code = Orders::getOrderColumn($this->order_id, 'order_code');
        $this->user_id = Orders::getOrderColumn($this->order_id, 'user_id');
        $shop_id = Orders::getOrderColumn($this->order_id, 'shop_id');

        //确保这个订单为该卖家的订单
        if(!$this->order_code || $this->shop_id != $shop_id){
            return $this->error(ErrorConst::NO_DATA_CODE_MSG, ErrorConst::NO_DATA_CODE);
        }

        //用户只能操作自身所属数据
        $this->user_comment_repo->scopeQuery(function ($query){
            return $query->where(['order_code' => $this->order_code, 'shop_id' => $this->shop_id]);
        });
    }

    public function index()
    {
        $user_comments = $this->user_comment_repo->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $user_comments, true);
    }

    public function show()
    {
        //确保这个订单为该卖家的订单
        $shop_id = Orders::getOrderColumn($this->order_id, 'shop_id');

        $user_comment=$this->user_comment_repo->scopeQuery(function ($query){
            return $query->where(['order_code' => $this->order_code, 'shop_id' => $this->shop_id]);
        })->all();

        if($user_comment['data']){
            $data['data']=$user_comment['data'][0];
            if($this->shop_id != $shop_id){
                $data['seller_is_comment']=0;
            }else{
                $data['seller_is_comment']=1;
            }
            return $this->success(ErrorConst::SUCCESS_CODE_MSG, $data);
        }else{
            if($this->shop_id != $shop_id){
                $user_comment['seller_is_comment']=0;
            }else{
                $user_comment['seller_is_comment']=1;
            }
            return $this->success(ErrorConst::SUCCESS_CODE_MSG, $user_comment);
        }
    }

    public function store(Request $request, OrdersRepository $order_repo)
    {
        $this->validate($request, [
            'overall' => 'required|integer',
            'comment' => 'required|string',
        ]);
        $data = $request->all();

        $data['order_code'] = $this->order_code;
        $data['shop_id'] = $this->shop_id;
        $data['user_id'] = $this->user_id;

        \DB::beginTransaction();
        try{
            $this->user_comment_repo->create($data);
            $order_repo->update(['seller_comment_status' => Orders::SELLER_COMMENT_STATUS_COMPLETED], $this->order_id);
            $ret = true;
            \DB::commit();
        }catch(\Exception $e){
            \DB::rollback();
            $ret = false;
        }

        return $this->noDataReturn($ret ? true : false);
    }

    public function update(Request $request, $id)
    {

    }

    public function delete($id)
    {

    }
}
